/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package is.bean.entity.xdumper.session;

import is.bean.entity.BeanRoutinesIntegerId;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.ejb.CreateException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import nullutil.NullUtil;

/**
 *
 * @author aspr0
 */
public class XdumperSessionBean extends BeanRoutinesIntegerId implements EntityBean
{
    private XdumperSession data = new XdumperSession();

    @Override
    public void setEntityContext(EntityContext aContext)
    {
        super.setEntityContext(aContext);
        tableName = "xdumper_session";
    }

    public Integer ejbCreateInstance(XdumperSession data) throws CreateException, SQLException
    {
        Connection connection = null;
        PreparedStatement statement = null;
        Integer createdId = null;
        ResultSet resultSet = null;

        try
        {
            connection = dataSource.getConnection();

            String sql = "select XDUMPER_SESSION_SEQ.nextval as id from dual";

            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            resultSet.next();
            createdId = resultSet.getInt("id");

            sql = "insert "
                    + "into xdumper_session "
                    + "(id, action, filepath, timestart) "
                    + "values "
                    + "(?, ?, ?, utsm2date(?))";

            statement = connection.prepareStatement(sql);

            statement.setInt(1, createdId);
            statement.setString(2, data.getAction());
            NullUtil.setStringNullDecide(statement, 3, data.getFilePath());
            statement.setLong(4, new Date().getTime());

            statement.execute();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            CreateException ce = new CreateException();
            ce.initCause(e);
            throw ce;
        }
        finally
        {
            if (statement != null)
            {
                statement.close();
            }

            if (connection != null)
            {
                connection.close();
            }

            if (resultSet != null)
            {
                resultSet.close();
            }
        }

        this.data = data;
        return createdId;
    }

    public void ejbPostCreateInstance(XdumperSession data)
    {
    }

    public XdumperSession getData()
    {
        return data;
    }

    public void setData(XdumperSession data)
    {
        this.data = data;
        modified = true;
    }

    @Override
    protected void loadData() throws SQLException
    {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try
        {
            connection = dataSource.getConnection();
            String sql =
                    "select "
                    + "action, filepath, date2utsm(timestart) as timestart, date2utsm(timeend) as timeend "
                    + "from xdumper_session "
                    + "where id = ?";
            statement = connection.prepareStatement(sql);

            id = (Integer) context.getPrimaryKey(); //todo resolve primary key after ejbCreate unset

            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            resultSet.next();

            data.setId(id);
            data.setAction(resultSet.getString("action"));
            data.setFilePath(resultSet.getString("filepath"));
            data.setTimeStart(resultSet.getLong("timestart"));
            data.setTimeEnd(resultSet.getLong("timeend"));
        }
        finally
        {
            if (resultSet != null)
            {
                resultSet.close();
            }

            if (statement != null)
            {
                statement.close();
            }
            if (connection != null)
            {
                connection.close();
            }
        }
    }

    @Override
    protected void storeData() throws SQLException
    {
        if (modified)
        {
            Connection connection = null;
            PreparedStatement statement = null;

            try
            {
                connection = dataSource.getConnection();
                String sql = "update xdumper_session set filepath = ?, timeend = utsm2date(?) where id = ?";
                statement = connection.prepareStatement(sql);

                NullUtil.setStringNullDecide(statement, 1, data.getFilePath());
                NullUtil.setLongNullDecide(statement, 2, data.getTimeEndLong());
                statement.setInt(3, id);
                statement.execute();
            }
            finally
            {
                if (statement != null)
                {
                    statement.close();
                }

                if (connection != null)
                {
                    connection.close();
                }
            }
        }
    }
}
